//|||||||||||||||||||||||||||||||||||||||||||||||

#include "Game1FinishedState.hpp"

//|||||||||||||||||||||||||||||||||||||||||||||||

using namespace Ogre;

//|||||||||||||||||||||||||||||||||||||||||||||||

Game1FinishedState::Game1FinishedState()
{
	m_bQuit = false;
	m_bGameWon = false;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void Game1FinishedState::enter()
{
    OgreFramework::getSingletonPtr()->m_pLog->logMessage("Entering Game1FinishedState...");

    m_pSceneMgr = OgreFramework::getSingletonPtr()->m_pRoot->createSceneManager(ST_GENERIC, "MenuSceneMgr");
    m_pSceneMgr->setAmbientLight(Ogre::ColourValue(0.7f, 0.7f, 0.7f));

    m_pCamera = m_pSceneMgr->createCamera("Game1FinishedMenuCam");
    m_pCamera->setPosition(Vector3(0, 25, -50));
    m_pCamera->lookAt(Vector3(0, 0, 0));
    m_pCamera->setNearClipDistance(1);

    m_pCamera->setAspectRatio(Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualWidth()) /
        Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualHeight()));

    OgreFramework::getSingletonPtr()->m_pViewport->setCamera(m_pCamera);

    OgreFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
    OgreFramework::getSingletonPtr()->m_pTrayMgr->showFrameStats(OgreBites::TL_BOTTOMLEFT);
    OgreFramework::getSingletonPtr()->m_pTrayMgr->showLogo(OgreBites::TL_BOTTOMRIGHT);
    OgreFramework::getSingletonPtr()->m_pTrayMgr->showCursor();
	if(m_bGameWon)
	{
		OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel(OgreBites::TL_CENTER, "wonLbl", "Congratulation! You succeded to pass this level!", 450);
	}
	else
	{
		OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel(OgreBites::TL_CENTER, "loseLbl", "You failed to pass this level!", 450);
	}
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_CENTER, "playBtn", "Play Again", 250);
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_CENTER, "SaveBtn", "Save Game", 250);
    OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_CENTER, "ExitBtn", "Quit", 250);
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void Game1FinishedState::exit()
{
    OgreFramework::getSingletonPtr()->m_pLog->logMessage("Leaving MainMenuState...");

    m_pSceneMgr->destroyCamera(m_pCamera);
    if(m_pSceneMgr)
        OgreFramework::getSingletonPtr()->m_pRoot->destroySceneManager(m_pSceneMgr);

    OgreFramework::getSingletonPtr()->m_pTrayMgr->clearAllTrays();
    OgreFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
    OgreFramework::getSingletonPtr()->m_pTrayMgr->setListener(0);
}

//|||||||||||||||||||||||||||||||||||||||||||||||


bool Game1FinishedState::keyPressed(const OIS::KeyEvent &keyEventRef)
{
	if(OgreFramework::getSingletonPtr()->m_pKeyboard->isKeyDown(OIS::KC_ESCAPE))
    {
        m_bQuit = true;
        return true;
    }

    OgreFramework::getSingletonPtr()->keyPressed(keyEventRef);
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool Game1FinishedState::keyReleased(const OIS::KeyEvent &keyEventRef)
{
    OgreFramework::getSingletonPtr()->keyReleased(keyEventRef);
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool Game1FinishedState::mouseMoved(const OIS::MouseEvent &arg)
{
    if(OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseMove(arg)) return true;
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool Game1FinishedState::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
    if(OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseDown(arg, id)) return true;
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool Game1FinishedState::mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
    if(OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseUp(arg, id)) return true;
    return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void Game1FinishedState::update(double timeSinceLastFrame)
{
	m_FrameEvent.timeSinceLastFrame = timeSinceLastFrame;
	OgreFramework::getSingletonPtr()->m_pTrayMgr->frameRenderingQueued(m_FrameEvent);

	    if(m_bQuit == true)
    {
        shutdown();
        return;
    }
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void Game1FinishedState::buttonHit(OgreBites::Button* button)
{
	if(button->getName() == "playBtn") {
        changeAppState(findByName("Game1State"));
	} else if(button->getName() == "SaveBtn") {
        changeAppState(findByName("GameProgressMenuState"));
	} else if(button->getName() == "ExitBtn") {
		m_bQuit = true;
	}
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void Game1FinishedState::setGameWon(bool gameWon)
{
	m_bGameWon = gameWon;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool Game1FinishedState::getGameWon()
{
	return m_bGameWon;
}

//|||||||||||||||||||||||||||||||||||||||||||||||